package org.pvt.gui;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import org.jetbrains.annotations.NotNull;
import org.pvt.R;
import org.pvt.model.HasName;
import org.pvt.model.Models;

import static org.pvt.model.Models.Event;


/**
 * @author Pavel Talanov
 */
public final class EventsActivity extends ListActivityWithMainMenu {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events);
        setListAdapter(Event.createCursorAdapter(this));
    }

    public void onAddClick(@NotNull View view) {
        EditText edit = (EditText) findViewById(R.id.addEventEdit);
        String text = edit.getText().toString();
        if (text.length() <= 3) {
            return;
        }
        addEvent(text);
        setListAdapter(Models.Event.createCursorAdapter(this));
        edit.setText("");
    }

    private void addEvent(@NotNull String text) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(HasName.NAME, text);
        Event.create(contentValues, this);
    }

    @Override
    protected void onListItemClick(android.widget.ListView l, android.view.View v, int position, long id) {
        UIUtils.startActivityForEvent(ExpensesActivity.class, id, this);
    }

    @Override
    public void onResume() {
        super.onResume();
        setListAdapter(Event.createCursorAdapter(this));
    }
}
